//
//  ViewController.m
//  TestAFNetworking
//
//  Created by ren on 2/5/17.
//  Copyright © 2017 ren. All rights reserved.
//

#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>

@interface ViewController ()
@property (nonatomic, strong) UITextView    *textView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setButtons];
    [self setTextView];
}

#pragma mark - add views
- (void)setButtons
{
    for (int i = 0; i < 20; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, 40 * i, 60, 40)];
        [btn setBackgroundColor:[UIColor redColor]];
        [btn setTitle:[NSString stringWithFormat:@"按钮%d",i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

- (void)setTextView
{
    [self.textView setFrame:CGRectMake(60, 0, [UIScreen mainScreen].bounds.size.width - 60, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:self.textView];
}

- (UITextView *)textView
{
    if (_textView == nil) {
        _textView = [UITextView new];
    }
    return _textView;
}

#pragma mark button action
- (void)btnClick:(UIButton *)button
{
    [self requestData];
}

#pragma mark - net work
- (void)requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    [manager.requestSerializer setValue:@"5AE222B7-4F09-4560-B43B-AE3DBC5C4E51" forHTTPHeaderField:@"idfa"];
    [manager.requestSerializer setValue:@"3.2.3" forHTTPHeaderField:@"apiVersion"];
    [manager.requestSerializer setValue:@"123123" forHTTPHeaderField:@"version"];
    [manager.requestSerializer setValue:@"IOS" forHTTPHeaderField:@"platform"];
    [manager.requestSerializer setValue:@"APPSTORE" forHTTPHeaderField:@"channelId"];
    [manager.requestSerializer setValue:@"iPhone Simulator" forHTTPHeaderField:@"device"];
    [manager.requestSerializer setValue:@"10.2" forHTTPHeaderField:@"sysVersion"];
    [manager.requestSerializer setValue:@"com.dmall.dmallmainapp" forHTTPHeaderField:@"appName"];
    [manager.requestSerializer setValue:@"667*375" forHTTPHeaderField:@"screen"];
    [manager.requestSerializer setValue:@"5AE222B7-4F09-4560-B43B-AE3DBC5C4E51" forHTTPHeaderField:@"uuid"];
    [manager.requestSerializer setValue:@"ac" forHTTPHeaderField:@"xyz"];
    
    
    [manager.requestSerializer setValue:@"appapi_b51257a043c206679f482b7952791612" forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"230" forHTTPHeaderField:@"storeId"];
    [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"venderId"];
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%d",1] forHTTPHeaderField:@"smartLoading"];//智能加载开关 0-关闭, 1-打开
    [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"networkType"];//网络类型：1-wifi, 2-2G, 3-3G/4G, 4-其它
    [manager.requestSerializer setValue:@"1486261648323" forHTTPHeaderField:@"currentTime"];
    
    [manager.requestSerializer setValue:@"123123" forHTTPHeaderField:@"lat"];
    [manager.requestSerializer setValue:@"12312312" forHTTPHeaderField:@"lng"];
    [manager.requestSerializer setValue:@"123123" forHTTPHeaderField:@"gatewayCache"];
    [manager.requestSerializer setValue:@"1-230-1;2-230-1;" forHTTPHeaderField:@"storeGroup"];
    [manager.requestSerializer setValue:@"H4sIAAAAAAAAA02Pu26EMBBFfyWyUq4QYPAauhSpF4X0yIbxMgrYlh+Loij/HkOxpD1zH3N/SIw4kZbIsuKUAmMlcOAyl4JL3nDZQK0KJYqaKVCSUnIhr8YPD3AejU7GIs/KBMdZaA1LAm9d13/ePt4TDLiCD2K1pC0qzkpWsKqpaZ0y/OgA9DAD3udAWsauR3Dy461P1ujBDcdn5FRvOIWZtPSaEhZzR30qVjMd7djNRsNLj2tcRDBuv2kIm3FfQ/i2kCT5zoS1/0bQrMwOvKFC0iqxeLgQn/zwrBjPLqGjEmOIDtw+2NoFEn+Anp5P//4BP9QTQFoBAAA=" forHTTPHeaderField:@"bigdata"];
    
    //    NSError *error;
    
    NSString *url = @"http://rbyyydev.com/";
    
    NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"keyword", @"name",
                             @"categoryId", @"10880",
                             @"sortRule", @"0",
                             @"sort", @"0",
                             @"pageNum", @"1",
                             @"businessCode", @"1",
                             @"pageSize", @"1",
                             @"from", @"1",
                             @"customParamStr", @"1",
                             @"s", @"1",
                             @"erpStoreId", @"112",
                             @"venderId", @"1",
                             @"brandId", @"1",
                             @"categoryLevel", @"1",
                             @"sortKey", @"1",
                             @"thirdCategoryId", @"1",
                             @"stores", @"1",
                             @"categoryType", @"1",
                             @"globalSelection", @"1",
                             nil];
    
    [[manager POST:url parameters:mapData progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@", uploadProgress.description);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.textView setText:[NSString stringWithFormat:@"%@",responseObject]];
            [self.textView setTextColor:[UIColor blackColor]];
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.textView setText:[NSString stringWithFormat:@"%@",error.userInfo.description]];
            [self.textView setTextColor:[UIColor redColor]];
        });
    }] resume];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
